#!/bin/sh
#update ssr subscript
#2019/12/26
#kevin

#counter
count=0
#tempcount
tempcount=4
#sscounter
sscount=0

#several important key
#ssr url
url=http://isx.yt/
#html file path
dir=/root/ssr

################ssr ip

ip=

################ssr port

port=

################ssr method

method=aes-256-cfb

################ssr protocol

protocol=origin

################ssr mix

mix=plain

################ssr password

password=

################ssr obfsparameter

obfsparameter=

################ssr protoparameter

protoparameter=

################ssr remark

remark=$ip

################ssr group

group=ishadow

ssrcontent=`echo $ip:$port:$protocol:$mix:$password/?obfsparam=$(printf "%s" $obfsparameter | base64)&protoparam=$(printf "%s" $protoparameter | base64)&remarks=$(printf "%s" $remark | base64)`
#echo $ssrcontent


#is exsit dir 
if [ ! -d $dir ];then
	mkdir $dir
fi

#is exsit html
if [ -f "$dir/index.html" ];then
	rm -fr $dir/index.html
fi
#is exsit s.txt
if [ -f "$dir/s.txt" ];then                                                                                                                                                                 
        rm -fr $dir/s.txt                                                                                                                                                               
fi 
#is exsit ssr.txt
if [ -f "$dir/ssr.txt" ];then                                                                                                                                                                      
        rm -fr $dir/ssr.txt                                                                                                                                                                        
fi  

#start get ssr info html
wget -qP $dir --user-agent=Mozilla/5.0 $url
if [ $? != 0 ];then
	echo "time:`date` downloading html error!" >> $dir/ssrupdate.log
	/root/getSSR.sh
	exit
fi

#get ssr info from html
cat $dir/index.html | grep -E 'h4' | sed -r 's/<.?h4>//g; s/<i.*>//g; s/<!.*>//g;s/<\/span.*>//g;s/<span.*>//g;s/<a.*//g;s/.*a>//g;s/<span.*//g;s/.*>.*//g' | grep -v '^ *$'|sed -r 's/ *//g' > $dir/ssr.txt

#deal with several values and link words and transform into base64
for line in `cat $dir/ssr.txt`;  
do    
	#partition key and value
	key=`echo $line | cut -d ':' -f1`
	value=`echo $line | cut -d ':' -f2`
	#echo $key $value

	#evaluation
   	if [ $key == "Password" ];then
		#base64Value=`echo $value | base64`
		#echo  $line | sed "s/$value/$base64Value/g"
		password=$value
	fi
	if [ $key == "IPAddress" ];then
		ip=$value
	fi
	if [ $key == "Port" ];then
		port=$value
	fi
	if [ $key == "Method" ];then
		method=$value
	fi
	if [ $key == "auth_sha1_v4tls1.2_ticket_auth" ];then
		mix=tls1.2_ticket_auth
		protocol=auth_sha1_v4	
	fi
	#counter add one 
	count=$(( $count + 1 ))
	
	#deal with one data 
	if [ $count == $tempcount ];then

		#change count for one for data
		sscount=$(( $sscount + 1 ))
		if [ $sscount == 6 ];then
			tempcount=5
		fi 
		#add remarks
		remark=$ip
		
		#make up ssrcontent
		ssrcontent=`echo $ip:$port:$protocol:$method:$mix:$(printf "%s" $password | base64)/?obfsparam=$(printf "%s" $obfsparameter | base64)\&protoparam=$(printf "%s" $protoparameter | base64)\&remarks=$(printf "%s" $remark | base64)\&group=$(printf "%s" $group | base64)`		
		ssrcontent=`echo $ssrcontent | sed 's/==//g'`
		#echo $ssrcontent
		content=`echo "ssr://$(printf $ssrcontent| base64)"`
		echo $content | sed 's/ //g' >> $dir/s.txt
		#echo $content >> $dir/s.txt
		
		#reset count 
		count=0
	fi

done

#transform s.txt into base64 info of ssr ssr.txt
cat $dir/s.txt | base64 | tr '\n' ' '| sed 's/ //g' > /www/ssr.txt 
